package de.jmda.core.util.ramcomp;

import java.net.URI;

import javax.tools.SimpleJavaFileObject;

/**
 * Represents an in memory java source code file (to be compiled).
 *
 * @author roger@jmda.de
 */
public class JavaSourceCodeObject extends SimpleJavaFileObject
{
	/**
	 * Represents java source code (to be compiled).
	 */
	private CharSequence sourceCode;

	/**
	 * Creates <code>string:///</code> {@link URI} based on <code>classname</code>
	 * and calls super constructor with that. Stores <code>code</code> in {@link
	 * #sourceCode}.
	 *
	 * @param classname Qualified name of the public class in <code>code</code>.
	 * @param sourceCode source code (to be compiled)
	 */
	public JavaSourceCodeObject(String classname, CharSequence sourceCode)
	{
		super(
				URI.create(
						"string:///" + classname.replace('.', '/') + Kind.SOURCE.extension),
				Kind.SOURCE);
		this.sourceCode = sourceCode;
	}

	/**
	 * @return {@link #sourceCode}
	 */
	@Override
	public CharSequence getCharContent(boolean ignoreEncodingErrors)
	{
		return sourceCode;
	}
}